package com.wx.qiqiai.handle;

import java.util.Hashtable;
import java.util.Map;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.TimeUnit;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.wx.qiqiai.cache.AnswerHistory;
import com.wx.qiqiai.common.Constant;
import com.wx.qiqiai.service.CharGptService;

import lombok.SneakyThrows;
import me.chanjar.weixin.mp.bean.message.WxMpXmlMessage;
import me.chanjar.weixin.mp.bean.message.WxMpXmlOutMessage;

/**
 * 文本消息处理类
 * 
 * @author zhugh
 * @date 2023/04/04 21:51
 **/
@Service("textHandle")
public class TextMsgHandle implements Handle {
    /**
     * 存储问题答案
     */
    private Map<String, BlockingQueue<String>> answerMap = new Hashtable<>();

    /**
     * 记录用户是否提问
     */
    private Map<String, String> questionMap = new Hashtable<>();

    @Autowired
    private CharGptService charGptService;

    @Autowired
    private AnswerHistory answerHistory;

    @SneakyThrows
    @Override
    public WxMpXmlOutMessage handle(WxMpXmlMessage inMessage) {
        if (inMessage.getContent().equals(Constant.GET_ANSWER_COMMAND)) {
            // 判断是否提问过
            if (questionMap.containsKey(inMessage.getFromUser())) {
                // 判断问题是否存在答案
                String element = answerMap.get(inMessage.getFromUser()).poll(4, TimeUnit.SECONDS);
                if (element == null) {
                    return createOutMessage("柒柒正在苦思冥想，请再等待几秒获取！", inMessage);
                } else {
                    questionMap.remove(inMessage.getFromUser());
                    return createOutMessage(element, inMessage);
                }
            } else {
                return createOutMessage("请向柒柒提问吧！", inMessage);
            }
        } else if (inMessage.getContent().equals(Constant.CLEAR_ANSWER_CONTEXT)) {
            answerHistory.clear(inMessage.getFromUser());
            return createOutMessage("上下文已清理", inMessage);
        }

        if (questionMap.containsKey(inMessage.getFromUser())) {
            // 存在未完成问题
            return createOutMessage("请先回复\"1\"获取上一问题的答案先呦！", inMessage);
        }

        /*
         * 初始化用户数据
         */
        if (!answerMap.containsKey(inMessage.getFromUser())) {
            answerMap.put(inMessage.getFromUser(), new ArrayBlockingQueue<>(10));
        }
        // 标记用户提问
        questionMap.put(inMessage.getFromUser(), "");

        // 调用接口获取答案
        charGptService.sendMsg(inMessage.getContent(), inMessage.getFromUser(), answerMap);

        // 等到接口返回答案
        String element = answerMap.get(inMessage.getFromUser()).poll(4, TimeUnit.SECONDS);
        if (element == null) {
            return createOutMessage("问题有点复杂，请等待几秒后，回复\"1\"获取答案哦", inMessage);
        } else {
            questionMap.remove(inMessage.getFromUser());
            return createOutMessage(element, inMessage);
        }
    }

    private WxMpXmlOutMessage createOutMessage(String context, WxMpXmlMessage inMessage) {
        return WxMpXmlOutMessage.TEXT().content(context).fromUser(inMessage.getToUser()).toUser(inMessage.getFromUser())
            .build();
    }
}
